package com.jiulan.hearthearing.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.ui.login.adapter.GuideAdapter;
import com.jiulan.hearthearing.ui.login.adapter.GuideDianAdapter;
import com.jiulan.hearthearing.ui.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 导航界面
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class GuideActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    private int currentItem = 0;
    private boolean isMove = false;
    private RecyclerView rvGuide;
    private ViewPager vpGuide;
    private GuideDianAdapter dianAdapter;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, GuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {
        vpGuide = findViewById(R.id.vpGuide);
        rvGuide = findViewById(R.id.rvGuide);
        setAdapter();
        setData();
    }

    private void setAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvGuide.setLayoutManager(manager);
        dianAdapter = new GuideDianAdapter(new ArrayList<>());
        dianAdapter.setOnItemClickListener(this);
        rvGuide.setAdapter(dianAdapter);
    }

    /**
     * 设置轮播图
     */
    private void setData() {
        final List<Integer> urls = new ArrayList<>();//banner
        urls.add(R.mipmap.guide_1);
        urls.add(R.mipmap.guide_2);
        urls.add(R.mipmap.guide_3);
        dianAdapter.setNewData(urls);
        vpGuide.setAdapter(new GuideAdapter(getSupportFragmentManager(), urls));
        vpGuide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                dianAdapter.setmPosition(position);
                if (currentItem == urls.size() - 1) isMove = true;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        final WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        vpGuide.setOnTouchListener(new View.OnTouchListener() {
            float startX = 0;
            float startY;
            float moveX;
            float moveY;
            float endX;
            float endY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Point size = new Point();

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (startX == 0) startX = event.getX();
                        moveX = event.getX();
                        moveY = event.getY();
                        windowManager.getDefaultDisplay().getSize(size);

                        if (currentItem == (urls.size() - 1) && size.x > 0 && startX - moveX >= (size.x / 5)) {
                            goToMainActivity();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        endX = event.getX();
                        endY = event.getY();
                        windowManager.getDefaultDisplay().getSize(size);

                        //首先要确定的是，是否到了最后一页，然后判断是否向左滑动，并且滑动距离是否符合，我这里的判断距离是屏幕宽度的4分之一（这里可以适当控制）
                        if (currentItem == (urls.size() - 1) && size.x > 0 && startX - endX >= (size.x / 5)) {
                            goToMainActivity();
                        }
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    public void goToMainActivity() {
        if (isMove) {
            isMove = false;
            MainActivity.actionStart(getContext(), 0);
            finish();
            overridePendingTransition(R.anim.activity_open, R.anim.activity_puse);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        vpGuide.setCurrentItem(position);
    }
}
